package kr.co.medical.controller;

import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
/*import kr.co.medical.dao.DoctorDao;*/
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpSession;

import kr.co.medical.dao.DoctorDao;
import kr.co.medical.dao.OfficeDao;
import kr.co.medical.dto.CalBean;
import kr.co.medical.dto.Calendar;
import kr.co.medical.dto.CheobangTempInsert;
import kr.co.medical.dto.Cheobang_All;
import kr.co.medical.dto.Cheobang_waiting_list;
import kr.co.medical.dto.Disease;
import kr.co.medical.dto.DiseaseInfo;
import kr.co.medical.dto.Doctormainview;
import kr.co.medical.dto.Dose;
import kr.co.medical.dto.Fee;
import kr.co.medical.dto.Filesview;
import kr.co.medical.dto.Geomsa_code;
import kr.co.medical.dto.Jindanseo;
import kr.co.medical.dto.Medicine;
import kr.co.medical.dto.Medicine_Cheobang_Patient;
import kr.co.medical.dto.Seosik;
import kr.co.medical.dto.Soonap;
import kr.co.medical.dto.Testmove;
import kr.co.medical.dto.Wardlist;

import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJacksonJsonView;

@Controller
public class DoctorController {
	@Autowired
	private SqlSession sqlSessionTemplate;
	
	
	
	
	@RequestMapping("doctor.medic")
	public ModelAndView doc_cheabang(HttpSession session, ModelAndView mav) throws ClassNotFoundException, SQLException {
		String doctor_id = (String) session.getAttribute("doctor_id2");
		mav.setViewName("doctor.doctor");
		DoctorDao doctordao = sqlSessionTemplate.getMapper(DoctorDao.class);
		List<Cheobang_waiting_list> list = doctordao.waitinglist(doctor_id);
		int count = doctordao.countlist(doctor_id);
		System.out.println(count);
		mav.addObject("count", count);
		mav.addObject("list", list);
		List<Dose> doselist = doctordao.selectdose();
		mav.addObject("doselist", doselist);
		return mav;
	}

	/* 처방 대기리스트 삭제 */
	@RequestMapping("waitlistdel.do")
	public ModelAndView waitinglist_del(ModelAndView mav, String delid) throws ClassNotFoundException, SQLException {
		mav.setViewName("doctor.doctor");
		String jupsoo_id = delid;
		DoctorDao doctordao = sqlSessionTemplate.getMapper(DoctorDao.class);
		doctordao.waitinglist_del(jupsoo_id);
		return mav;
	}

	/* 처방 대기리스트 삭제 후 삭제된것 외의 리스트를 뿌려줌 */
	@RequestMapping("rewaitlist.do")
	public ModelAndView waitinglist_del(ModelAndView mav,

	HttpSession session) throws ClassNotFoundException, SQLException {
		String doctor_id = (String) session.getAttribute("doctor_id2");
		DoctorDao doctordao = sqlSessionTemplate.getMapper(DoctorDao.class);
		List<Cheobang_waiting_list> rewaitlist = new ArrayList<Cheobang_waiting_list>();
		rewaitlist = doctordao.waitinglist(doctor_id);
		for (int i = 0; i < rewaitlist.size(); i++) {
			System.out.println("rewaitlist.get(i).getName() : "
					+ rewaitlist.get(i).getName());
		}
		mav = new ModelAndView(new MappingJacksonJsonView());
		mav.addObject("rewaitlist", rewaitlist);
		System.out.println("rewaitlist.size()2 : " + rewaitlist.size());

		return mav;
	}

	/* 진료버튼 눌렀을때 환자 정보를 받아옴 */
	@RequestMapping("doctor2.do")
	public ModelAndView chebang_click(ModelAndView mav, String patientjupsooid,
			String delid) throws ClassNotFoundException, SQLException {
		System.out.println(patientjupsooid);
		System.out.println(delid);
		DoctorDao doctordao = sqlSessionTemplate.getMapper(DoctorDao.class);
		Doctormainview doctormainview = doctordao.doctormaininfo(patientjupsooid);
		mav = new ModelAndView(new MappingJacksonJsonView());
		doctordao.waitinglist_del(delid);
		mav.addObject("patientinfo", doctormainview);
		return mav;

	}

	/* 서식지 리스트 */
	@RequestMapping("seosik.do")
	public ModelAndView seosik(ModelAndView mav) throws ClassNotFoundException,
			SQLException {
		DoctorDao doctordao = sqlSessionTemplate.getMapper(DoctorDao.class);
		List<Seosik> seosik = doctordao.seosik();
		mav = new ModelAndView(new MappingJacksonJsonView());
		mav.addObject("seosik", seosik);
		return mav;
	}

	/* -------------서식지 검색------- */

	@RequestMapping("seosiksearch.do")
	public ModelAndView seosiksearch(ModelAndView mav, String seosiksearch)
			throws ClassNotFoundException, SQLException {
		System.out.println(seosiksearch);
		DoctorDao doctordao = sqlSessionTemplate.getMapper(DoctorDao.class);
		List<Seosik> seosiksearch1 = doctordao.seosiksearch(seosiksearch);
		mav = new ModelAndView(new MappingJacksonJsonView());
		mav.addObject("seosiksearch", seosiksearch1);
		return mav;

	}
	@RequestMapping("jusalist.do")
	@ResponseBody
	public HashMap<String,Object> jusalist() throws ClassNotFoundException, SQLException{
		DoctorDao doctordao = sqlSessionTemplate.getMapper(DoctorDao.class);
		List<Fee> list = new ArrayList<Fee>();
		list = doctordao.jusa();
		HashMap<String,Object> hashmap = new HashMap<String, Object>();
		hashmap.put("list", list);
		return hashmap;
	}
	
	@RequestMapping("tempinsert.do")
	@ResponseBody
	public String tempinsert(String jupsoo_id,String habitat_detail_id) throws ClassNotFoundException, SQLException{
		DoctorDao doctordao = sqlSessionTemplate.getMapper(DoctorDao.class);
		doctordao.tempinsert(jupsoo_id,habitat_detail_id);
		CheobangTempInsert cheobang = doctordao.selectcheobang_tempid(jupsoo_id);
		String cheobang_temp_id = cheobang.getCheobang_temp_id();
		return cheobang_temp_id;
	}
	
	@RequestMapping("cheobang_insert.medic")
	public String cheobang_insert(CheobangTempInsert cheobang,HttpSession session,String dose_id)  throws ClassNotFoundException, SQLException{
		DoctorDao doctordao = sqlSessionTemplate.getMapper(DoctorDao.class);
		List<Geomsa_code> list = new ArrayList<Geomsa_code>();
		String cheobang_id = cheobang.getCheobang_temp_id();
		String fee_id=cheobang.getFee_id();
		String jupsoo_id = cheobang.getJupsoo_id();
		String geomsa_code_id = cheobang.getGeomsa_code_id();
		String cheobang_temp_id = cheobang_id;
		String patient_id = cheobang.getPatient_id();
		list = doctordao.examinesel(cheobang_id);
		if(list.size() >0){
			cheobang.setGeomsa_code_id(list.get(0).getGeomsa_code_id());
		}else{
			doctordao.examineinsert(cheobang_temp_id, geomsa_code_id, fee_id);
		}
		
		OfficeDao officedao = sqlSessionTemplate.getMapper(OfficeDao.class);
		
		System.out.println(cheobang_temp_id);
		System.out.println(dose_id);
		int i = doctordao.medidose(cheobang_temp_id, dose_id);
		if(i>0){
			for(int v=0;v<medilist.size();v++){
				doctordao.insertmedic(medilist.get(v).getMedicine_id(),cheobang_temp_id,medilist.get(v).getQuantity());
			}
		}
		
		//급여 진찰료 구하기
		int ok_sg_price1=doctordao.ok_sg_price1(cheobang_id);
		// 수가 구하기
		String sooga_name="내복약";
		String sooga_name2="검사진료";
		//약수가
		int sooga=doctordao.gagam_sooga(sooga_name);
		//검사 수가
		int sooga2=doctordao.gagam_sooga(sooga_name2);
		
		//수가 적용된 급여 진찰료
		ok_sg_price1 -=ok_sg_price1*sooga2/100;
		
		//급여 약 값 구하기
		int ok_sg_price_medicine=doctordao.ok_sg_price2(cheobang_id);
		ok_sg_price_medicine -= ok_sg_price_medicine*sooga/100;
		
		//급여 진찰료 + 급여 약 값= 총급여
		ok_sg_price1 = ok_sg_price1+ok_sg_price_medicine;
		
		//비급여 진찰료 구하기
		int no_sg_price1 = doctordao.no_sg_price1(cheobang_id);
		
		
		
		
	
		
		//과거 진료 접수 날짜 세션에서 받아옴
		
		//현재 날짜 구하기
		String yyMMdd = new SimpleDateFormat("yyyy-MM-dd").format(new Date()).toString();
		String yyyy =  new SimpleDateFormat("yyyy").format(new Date()).toString();
		String MM = new SimpleDateFormat("MM").format(new Date()).toString();
		String dd = new SimpleDateFormat("dd").format(new Date()).toString();
		String Cho="";
		String lastjupsoodate = doctordao.selectjupsoo_date(patient_id);
			if(lastjupsoodate.equals(yyMMdd)){
				Cho = "N";
			}else{
				//40일 이전 날짜 구하기
				Calendar cal=doctordao.selectcal(Integer.parseInt(yyyy),Integer.parseInt(MM),Integer.parseInt(dd));
				String pastcalendar_id=cal.getCalendar_id();
				System.out.println("40일 이전 날짜 구하기"+pastcalendar_id);
				
				
				//현재 calendar_id구하기
				String calendar_id=doctordao.selectcal2(Integer.parseInt(yyyy), Integer.parseInt(MM), Integer.parseInt(dd));

				
				List<Calendar> cal_list=doctordao.cal_list(pastcalendar_id,calendar_id);
				List<String> diseaselist = new ArrayList<String>();
				List<String> diseaselist2 = new ArrayList<String>();

				for(int k=0 ;k< cal_list.size();k++){
					System.out.println(cal_list.get(k).getSdate());
					diseaselist = doctordao.is_re_check(cal_list.get(k).getSdate(), patient_id);
				}
				for(int x=0;x<diseaselist.size();x++){
					System.out.println(diseaselist.get(x));
					diseaselist2 = doctordao.disease_Check(cheobang_id, diseaselist.get(x));
				}
				if(diseaselist2.size()>0){
					Cho="Y";
				}else{
					Cho="N";
				}
				System.out.println(Cho);
			}
		Soonap soonap = new Soonap();
		
		//-------------------------------------
		
		
		int money = 0;
		soonap=doctordao.is_soonap(jupsoo_id);
		if(Cho.equals("Y")){
			money=doctordao.select_fee("2");	
		}else if(Cho.equals("N")){
			money=doctordao.select_fee("1");
		}
		double money2 = money;
		if(soonap.getIs_holiday().equals("Y") ){
			money2 = money + money*0.7;
		}
		if(soonap.getIs_night().equals("Y")){
			money2 += money*0.5;
		}
		
		
		ok_sg_price1=ok_sg_price1+money;
		

		int total_price =ok_sg_price1 + no_sg_price1;
		int total_sg_price = (int) (total_price+money2-money);
		
		
		
		soonap.setOk_sg_price(ok_sg_price1);

		soonap.setNo_sg_price(no_sg_price1);
		soonap.setTotal_price(total_price);
		soonap.setTotal_sg_price(total_sg_price);
		soonap.setIs_re(Cho);
		soonap.setJupsoo_id(jupsoo_id);
		soonap.setCheobang_id(cheobang_id);
		doctordao.cheobangcompleInsert(cheobang);
		doctordao.soonapfinal(soonap);

		doctordao.no_soonap_insert(soonap);

		//officedao.soogainsert(cheobang_id);
		
		
		return "redirect:/doctor.medic";
		
	}
	
	public ArrayList<Medicine_Cheobang_Patient> medilist = new ArrayList<Medicine_Cheobang_Patient>();
	@RequestMapping("mediaddlist.medic")
	@ResponseBody
	public String mediaddlist(String medicode,String mediamount) throws ClassNotFoundException, SQLException{
		DoctorDao doctordao = sqlSessionTemplate.getMapper(DoctorDao.class);
		Medicine_Cheobang_Patient medicine;
		medicine = new Medicine_Cheobang_Patient();
		medicine.setMedicine_id(medicode);
		medicine.setQuantity(mediamount);
		System.out.println(medicine.getMedicine_id() +"/"+ medicine.getQuantity());
		medilist.add(medicine);
		return "success";
	}
	
	@RequestMapping("medidose.medic")
	@ResponseBody
	public String medidose(String cheobang_temp_id,String dose_id) throws ClassNotFoundException, SQLException{
		DoctorDao doctordao = sqlSessionTemplate.getMapper(DoctorDao.class);
		System.out.println(cheobang_temp_id);
		System.out.println(dose_id);
		int i = doctordao.medidose(cheobang_temp_id, dose_id);
		if(i>0){
			for(int v=0;v<medilist.size();v++){
				doctordao.insertmedic(medilist.get(v).getMedicine_id(),cheobang_temp_id,medilist.get(v).getQuantity());
			}
		}
		return "success";
	}
	
	// //////////////////////서식지검색끝////////////////////////////////////

	/*----------------------상병 리스트-------------------------*/

	@RequestMapping("disease.do")
	public ModelAndView disease(ModelAndView mav)
			throws ClassNotFoundException, SQLException {
		DoctorDao doctordao = sqlSessionTemplate.getMapper(DoctorDao.class);
		List<Disease> disease = doctordao.disease();
		mav = new ModelAndView(new MappingJacksonJsonView());
		mav.addObject("disease", disease);
		return mav;
	}

	/*-----------------상병 검색----------------*/

	@RequestMapping("diseasesearch.do")
	public ModelAndView diseasesearch(ModelAndView mav, String diseasesearch)
			throws ClassNotFoundException, SQLException {
		System.out.println(diseasesearch);
		DoctorDao doctordao = sqlSessionTemplate.getMapper(DoctorDao.class);
		List<Disease> diseasesearch1 = doctordao.diseasesearch(diseasesearch);
		mav = new ModelAndView(new MappingJacksonJsonView());
		mav.addObject("diseasesearch", diseasesearch1);
		return mav;

	}

	// //////////////////////상병검색끝////////////////////////////////////

	/* -------------약 리스트-------------- */

	@RequestMapping("medicine.do")
	public ModelAndView medicine(ModelAndView mav)
			throws ClassNotFoundException, SQLException {
		DoctorDao doctordao = sqlSessionTemplate.getMapper(DoctorDao.class);
		List<Medicine> medicine = doctordao.medicine();
		mav = new ModelAndView(new MappingJacksonJsonView());
		mav.addObject("medicine", medicine);
		return mav;
	}

	/* ---------------------------- 약검색--------------------- */

	@RequestMapping("medisearch.do")
	public ModelAndView medisearch(ModelAndView mav, String medisearch)
			throws ClassNotFoundException, SQLException {
		System.out.println(medisearch);
		DoctorDao doctordao = sqlSessionTemplate.getMapper(DoctorDao.class);
		List<Medicine> medisearch1 = doctordao.medisearch(medisearch);
		mav = new ModelAndView(new MappingJacksonJsonView());
		mav.addObject("medisearch", medisearch1);
		return mav;

	}

	// //////////////////////약검색끝////////////////////////////////////

	/* ---------------------------- 검사실 이동 --------------------- */

	@RequestMapping(value = "testmove.do")
	public ModelAndView testmove(ModelAndView mav, String opinion, String jupsoo_id,
			String habitat_detail_id,String geomsa_code_id, String is_ward,String fee_id
			) throws ClassNotFoundException, SQLException {
		DoctorDao doctordao = sqlSessionTemplate.getMapper(DoctorDao.class);
		doctordao.testmove(opinion, jupsoo_id, habitat_detail_id, geomsa_code_id, is_ward); // /처방temp에 insert
		mav.setViewName("redirect:/doctor.medic");
		return mav;
	}
	

	// --------------------------------- 검사실이동2--------------------------//
	@RequestMapping("testmove2.do")
	public ModelAndView testmove2(ModelAndView mav, String jupsoo_id) {
		System.out.println(jupsoo_id);
		DoctorDao doctordao = sqlSessionTemplate.getMapper(DoctorDao.class);
		Testmove testmove = doctordao.testmove2(jupsoo_id);		
		mav = new ModelAndView(new MappingJacksonJsonView());
		mav.addObject("testmove2", testmove);
		return mav;

	}

	// --------------------------------- 검사실이동3--------------------------//

	@RequestMapping("testmove3.do")
	public ModelAndView testmove3(ModelAndView mav, String cheobang_temp_id,String fee_id,String geomsa_code_id)
			throws ClassNotFoundException, SQLException {
		DoctorDao doctordao = sqlSessionTemplate.getMapper(DoctorDao.class);
		doctordao.examineinsert(cheobang_temp_id, geomsa_code_id, fee_id);
		doctordao.testmove3(cheobang_temp_id); // /radio_waiting_list에 insert
		mav.setViewName("redirect:/doctor.medic");
		return mav;
	}

		
			/*  처방 아이디 뽑아옴  */
		@RequestMapping("cheobangselect1.do")
		public ModelAndView cheobangidselect(ModelAndView mav, String jupsoo_id){
			
			DoctorDao doctordao = sqlSessionTemplate.getMapper(DoctorDao.class);
			Testmove testmove2 = doctordao.testmove2(jupsoo_id);	
			mav = new ModelAndView(new MappingJacksonJsonView());	
			mav.addObject("testmove2", testmove2);	
			return mav;	
		}
			/*뽑아온 처방 ID로 처방 테이블에 insert*/
	@RequestMapping("cheobanginsert.do")
	public ModelAndView cheobang_insert(ModelAndView mav, String cheobang_temp, String diseasecode){
		System.out.println("처방탬프 아이디 : " + cheobang_temp + " 병 코드 : " + diseasecode);
		DoctorDao doctordao = sqlSessionTemplate.getMapper(DoctorDao.class);
		doctordao.cheobang_insert(cheobang_temp, diseasecode); //cheobang 테이블에 insert
		
		mav.setViewName("redirect:/doctor.medic");
		return mav;
	}
			
	
	/*  처방 버튼 클릭 액션 geomsa_code에서 geomsa_id  */
	@RequestMapping("geomsacode.do")
	public ModelAndView geomsacode_sel(ModelAndView mav, String jupsoo_id){
		DoctorDao doctordao = sqlSessionTemplate.getMapper(DoctorDao.class);
		System.out.println("검사코드 접수 id : " + jupsoo_id);
		List<CheobangTempInsert> geomsacodename =  doctordao.geomsacode_sel(jupsoo_id); //처방탬프 테이블에서 검사코드, 처방 코드를 가져옴
		mav = new ModelAndView(new MappingJacksonJsonView());	
		mav.addObject("geomsacode", geomsacodename);
		return mav;

	}
		@RequestMapping("geomsacodename.do")
		public ModelAndView geomsacodenamesel(ModelAndView mav, String geomsacodeid){
			DoctorDao doctordao = sqlSessionTemplate.getMapper(DoctorDao.class);
			Geomsa_code geomsacodename2 =  doctordao.geomsacodenamesel(geomsacodeid); 
			mav = new ModelAndView(new MappingJacksonJsonView());	
			mav.addObject("geomsaname1", geomsacodename2);
			return mav;
		}		
		@RequestMapping("examineinsert.do")
		public ModelAndView examineinsert(ModelAndView mav, String geomsaname, String cheobang_tempid){
			DoctorDao doctordao = sqlSessionTemplate.getMapper(DoctorDao.class);
			mav.setViewName("redirect:/doctor.medic");
			return mav;
		}
		
		@RequestMapping("cheobangcomplete.do")
		public ModelAndView cheobangcompleInsert(ModelAndView mav, String opinion, String jupsoo_id,
				String habitat_detail_id,String geomsa_code_id, String is_ward){
			DoctorDao doctordao = sqlSessionTemplate.getMapper(DoctorDao.class);
			doctordao.cheobangcompleInsert(opinion,  jupsoo_id, habitat_detail_id,	geomsa_code_id,  is_ward);
			mav.setViewName("redirect:/doctor.medic");
			return mav;
		}
		
	/*
	 * @RequestMapping("radiowaiting.do") public ModelAndView radiowaiting
	 */

	@RequestMapping("roomlist.medic")
	public ModelAndView roomlist(HttpSession session, ModelAndView mav)
			throws ClassNotFoundException, SQLException {

		String doctor_id = (String) session.getAttribute("doctor_id2");
	    
		String yyMMdd = new SimpleDateFormat("yyyy-MM-dd").format(new Date()).toString();
		String MM = new SimpleDateFormat("MM").format(new Date()).toString();
		String dd = new SimpleDateFormat("dd").format(new Date()).toString();
		
		
		mav.setViewName("doctor.roomlist");
		DoctorDao doctordao = sqlSessionTemplate.getMapper(DoctorDao.class);
		List<Cheobang_waiting_list> list = doctordao.waitinglist(doctor_id);
		
		List<Wardlist> wardlist = doctordao.selectwardlist(doctor_id);
		mav.addObject("wardlist", wardlist);

		for(int i=0;i<wardlist.size(); i++){
			//System.out.println(wardlist.get(i).getWard_date().substring(5,7));
			//월 구하기
			if(Integer.parseInt(wardlist.get(i).getWard_date().substring(5,7)) <= Integer.parseInt(MM)){
				int month=Integer.parseInt(MM)-Integer.parseInt(wardlist.get(i).getWard_date().substring(5,7));
				int warddate=Integer.parseInt(wardlist.get(i).getWard_date().substring(5,7));
				int maxmonth=doctordao.maxmonth(warddate);
				month=month*maxmonth;
				//입원 일수 구하는 if문
				int p=Integer.parseInt(dd)-Integer.parseInt(wardlist.get(i).getWard_date().substring(8,10));
				p=(month+p)+1;
				String w_soonap_id=wardlist.get(i).getW_soonap_id();
				doctordao.updateward_period(p,w_soonap_id);
				
			}
			
		}
		
		mav.addObject("list", list);
		return mav;
	}
	
	@RequestMapping("roomlist2.do")
	public ModelAndView roomlist2(HttpSession session)
			throws ClassNotFoundException, SQLException {
		String doctor_id = (String) session.getAttribute("doctor_id2");
		ModelAndView mav = new ModelAndView(new MappingJacksonJsonView());
		DoctorDao doctordao = sqlSessionTemplate.getMapper(DoctorDao.class);
		List<Cheobang_waiting_list> list = doctordao.waitinglist(doctor_id);
		List<Wardlist> wardlist = doctordao.selectwardlist(doctor_id);
		
		mav.addObject("wardlist", wardlist);
		mav.addObject("list", list);
		
		return mav;
	}
	
	@RequestMapping("schedule.medic")
	public ModelAndView schedule(HttpSession session, ModelAndView mav)
			throws ClassNotFoundException, SQLException {
		String doctor_id = (String) session.getAttribute("doctor_id2");
		System.out.println(doctor_id);

		mav.setViewName("doctor.d_schedule");
		DoctorDao doctordao = sqlSessionTemplate.getMapper(DoctorDao.class);
		List<Cheobang_waiting_list> list = doctordao.waitinglist(doctor_id);

		mav.addObject("list", list);

		return mav;
	}

	@RequestMapping("doctorschedule.medic")
	@ResponseBody
	public ArrayList<Object> doctorchedule(String id)
			throws ClassNotFoundException, SQLException {
		DoctorDao doctordao = sqlSessionTemplate.getMapper(DoctorDao.class);
		List<CalBean> callist = new ArrayList<CalBean>();
		ArrayList<Object> ja = new ArrayList<Object>();
		callist = doctordao.doctorschedule(id);
		for (int i = 0; i < callist.size(); i++) {
			CalBean calbean = callist.get(i);
			ja.add(calbean);
		}
		return ja;

	}
	
	@RequestMapping("wardout.do")
	@Transactional
	@ResponseBody
	public String wardout(Wardlist wardlist){
		String msg="yes";
		DoctorDao doctordao = sqlSessionTemplate.getMapper(DoctorDao.class);
		String jupsoo_id=wardlist.getJupsoo_id();
		String is_res="N";
		int total_price=doctordao.wardsoonaptotalselect(wardlist);
		String sooga_name="입원";
		int sooga=doctordao.gagam_sooga(sooga_name);
		total_price=total_price-total_price/sooga;
		wardlist.setIs_res(is_res);
		wardlist.setTotal_price(total_price);
		doctordao.wardsoonapinsert(wardlist);
		doctordao.wardnosoonapinsert(wardlist);
		doctordao.dischargeinsert(wardlist);
		doctordao.delectward_list(wardlist);
		doctordao.deletew_soonap(wardlist);
		return msg;
	}
	
	
	@RequestMapping("files.do")
	public ModelAndView filesview(String patient_id){
		DoctorDao doctordao = sqlSessionTemplate.getMapper(DoctorDao.class);
		List<Filesview> list = doctordao.filesview(patient_id);
		ModelAndView mav = new ModelAndView(new MappingJacksonJsonView());
		mav.addObject("filesview", list);
		
		return mav;
	}

	@RequestMapping("cheobang_select.do")
	public ModelAndView cheobang_select(String cheobang_temp_id, ModelAndView mav){
		System.out.println(cheobang_temp_id);
		DoctorDao doctordao = sqlSessionTemplate.getMapper(DoctorDao.class);
		
		return mav;
	}
	
	//진단서 버튼 클릭시 처방에 insert
	@RequestMapping("Jindanseo_cheoInsert.do")
	public ModelAndView cheobang_Insert(String opinion, String cheobang_temp_id, String jupsoo_id,
			String habitat_detail_id,String geomsa_code_id, String fee_id, ModelAndView mav){
		
		System.out.println("접수아이디2 : " +cheobang_temp_id + " / " +opinion+ " / " +habitat_detail_id+ " / " +geomsa_code_id);
		DoctorDao doctordao = sqlSessionTemplate.getMapper(DoctorDao.class);
		int a = doctordao.cheobang_Insert2(cheobang_temp_id, opinion,  jupsoo_id, habitat_detail_id, geomsa_code_id);

		mav.setViewName("redirect:/doctor.medic");
			
		return mav;
	}
	
	@RequestMapping("cheobang_id.do")
	public ModelAndView cheobang_id(String jupsoo_id, ModelAndView mav){
			DoctorDao doctordao = sqlSessionTemplate.getMapper(DoctorDao.class);
			Cheobang_All cheobangid =  doctordao.cheobang_id(jupsoo_id);
			mav = new ModelAndView(new MappingJacksonJsonView());
			mav.addObject("cheobang", cheobangid);
		return mav;
	}
	
	@RequestMapping("jindanseo_jupsoo_type_select.do")
	public ModelAndView jindanjupsooSelect(String jupsoo_id, ModelAndView mav){
		DoctorDao doctordao = sqlSessionTemplate.getMapper(DoctorDao.class);
		Cheobang_All type =  doctordao.cheobang_type(jupsoo_id);
		mav = new ModelAndView(new MappingJacksonJsonView());
		mav.addObject("type", type);
		
		return mav;
	}
	//상해 진단 접수 시작
	@RequestMapping("jindanseo_code_insert.do")
	public ModelAndView jindanseo_code_insert(String cheobang_id, ModelAndView mav){
		System.out.println("처방 아이디 : " + cheobang_id);
		DoctorDao doctordao = sqlSessionTemplate.getMapper(DoctorDao.class);
		int a = doctordao.jindanCode_Insert(cheobang_id);
		mav.setViewName("redirect:/doctor.medic");
		
		return mav;
	}
	
	@RequestMapping("injury_jindanseo_select.do")
	public ModelAndView injury_jindanseo_select(String cheobang_id, ModelAndView mav){
		System.out.println("cheobang_id가져와라 : "+cheobang_id);
		DoctorDao doctordao = sqlSessionTemplate.getMapper(DoctorDao.class);
		Jindanseo jindancodeid =  doctordao.injury_jindanseo_select(cheobang_id);
		mav = new ModelAndView(new MappingJacksonJsonView());
		mav.addObject("jindancodeid", jindancodeid);
		
		return mav;
	}
	
	@RequestMapping("jindanseo.do")
	public ModelAndView jindanseo_info(String jupsoo_id, ModelAndView mav){
		System.out.println("jupsoo_id : "+jupsoo_id);
		DoctorDao doctordao = sqlSessionTemplate.getMapper(DoctorDao.class);
		Jindanseo jindan =  doctordao.jindanseo_jupsoo_select(jupsoo_id);
		mav = new ModelAndView(new MappingJacksonJsonView());
		mav.addObject("jindan", jindan);
		
		return mav;
	}
	
	@RequestMapping("injury_jindanseo_Insert.do")
	public ModelAndView injury_jindanseo_Insert(String injury_date, 
												String jindanseo_code_id,
												ModelAndView mav) throws ParseException{
		System.out.println("진단서 코드id : " + jindanseo_code_id + " / Injury_date : " +injury_date);
		DoctorDao doctordao = sqlSessionTemplate.getMapper(DoctorDao.class);
		int a = doctordao.injury_jindanseo_Insert(injury_date, jindanseo_code_id);
		mav.setViewName("redirect:/doctor.medic");
		
		return mav;
	}
	
	//상해 진단접수 끝
	
	
	//사망 진단 접수 시작
	@RequestMapping("jindanseo_code_samang_insert.do")
	public ModelAndView jindanseo_code_samang_insert(String cheobang_id, ModelAndView mav){
		
		DoctorDao doctordao = sqlSessionTemplate.getMapper(DoctorDao.class);
		int a = doctordao.jindanseo_code_samang_insert(cheobang_id);
		mav.setViewName("redirect:/doctor.medic");
		
		return mav;
	}
	@RequestMapping("injury_jindanseo_samang_select.do")
	public ModelAndView injury_jindanseo_samang_select(String cheobang_id, ModelAndView mav){
		DoctorDao doctordao = sqlSessionTemplate.getMapper(DoctorDao.class);
		Jindanseo jindancodeid =  doctordao.injury_jindanseo_samang_select(cheobang_id);
		mav = new ModelAndView(new MappingJacksonJsonView());
		mav.addObject("jindancodeid", jindancodeid);
		
		return mav;
	}
	
	@RequestMapping("jindanseo_samang.do")
	public ModelAndView jindanseo_info2(String jupsoo_id, ModelAndView mav){
		/*SELECT * FROM jindanseo_jupsoo*/
		System.out.println("jupsoo_id : "+jupsoo_id);
		DoctorDao doctordao = sqlSessionTemplate.getMapper(DoctorDao.class);
		Jindanseo jindan =  doctordao.jindanseo_jupsoo_select2(jupsoo_id);
		mav = new ModelAndView(new MappingJacksonJsonView());
		mav.addObject("deathjindan", jindan);
		
		return mav;
	}
	@RequestMapping("injury_jindanseo_samang_Insert.do")
	public ModelAndView injury_jindanseo_samang_Insert(String death_date, String death_location, String jindanseo_code_id, ModelAndView mav) throws ParseException{
		
		System.out.println("진단서 코드id : " + jindanseo_code_id + " / death_date : " +death_date + " / death_location : " + death_location);
		DoctorDao doctordao = sqlSessionTemplate.getMapper(DoctorDao.class);
		int a = doctordao.injury_jindanseo_samang_Insert(death_location,death_date, jindanseo_code_id);
		mav.setViewName("redirect:/doctor.medic");
		
		return mav;
	}
	
	//사망 진단 접수 끝
	
	//탭2의 처방 내역 받아오기
	@RequestMapping("cheobang_history.do")
	public ModelAndView cheobang_history(String patient_id){
		ModelAndView mav = new ModelAndView(new MappingJacksonJsonView());
		System.out.println("patient_id : " + patient_id);
	
		DoctorDao doctordao = sqlSessionTemplate.getMapper(DoctorDao.class);
		List<Cheobang_All> list = doctordao.cheobang_history(patient_id);
		mav.addObject("list",list);
		return mav;
	}
	//탭2의 약처방 내역
	@RequestMapping("medi_cheobang.do")
	public ModelAndView medi_cheobang(String patient_id){
		ModelAndView mav = new ModelAndView(new MappingJacksonJsonView());
		System.out.println("patient_id : " + patient_id);
		DoctorDao doctordao = sqlSessionTemplate.getMapper(DoctorDao.class);
		List<Cheobang_All> list2 = doctordao.medi_cheobang(patient_id);
		mav.addObject("list2",list2);
		return mav;
	}
	
	
	//정보 컨트롤러
	@RequestMapping("diseaseinfo.do")
	public ModelAndView diseaseinfo() throws ClassNotFoundException, SQLException{
		ModelAndView mav = new ModelAndView(new MappingJacksonJsonView());
		DoctorDao doctordao = sqlSessionTemplate.getMapper(DoctorDao.class);
		List<DiseaseInfo> di=doctordao.diseaseinfo();
		mav.addObject("di",di);
		
		return mav;
	}

	@RequestMapping("cheobang_temp_Insert1.do")
	public ModelAndView cheobang_temp_Insert1(ModelAndView mav, String opinion, String jupsoo_id,
			String habitat_detail_id,String geomsa_code_id,String fee_id
			) throws ClassNotFoundException, SQLException {
		DoctorDao doctordao = sqlSessionTemplate.getMapper(DoctorDao.class);
		doctordao.cheobang_temp_Insert1(opinion, jupsoo_id, habitat_detail_id, geomsa_code_id); // /처방temp에 insert
		mav.setViewName("redirect:/doctor.medic");
		return mav;
	}
	
	@RequestMapping("cheobang_temp_Select2.do")
	public ModelAndView cheobang_temp_Select2(ModelAndView mav, String jupsoo_id) {
		System.out.println(jupsoo_id);
		DoctorDao doctordao = sqlSessionTemplate.getMapper(DoctorDao.class);
		List<Testmove> testmove = doctordao.cheobang_temp_idsel(jupsoo_id);		
		mav = new ModelAndView(new MappingJacksonJsonView());
		mav.addObject("cheobang_temp", testmove);
		return mav;

	}
	
	
	
}
